<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1.封装保存cookie的方法
        function setCookie(key, value, time) {
            // 获取目前的事件
            var passDate = new Date();
            // 设置过期的时间
            // passDate.setDate(passDate.getDate() + time);
            // 一个小时过期时间
            // passDate.setTime(passDate.getTime + 60 * 60 * 1000);
            // console.log(passDate.getTime() + time * 60 * 60 * 1000);
            passDate.setTime(passDate.getTime() + time * 60 * 60 * 1000);
            // 保存cookie
            document.cookie = key + '=' + value + ';expires=' + passDate;
        };
        // 调用
        setCookie('peng', '帅男一枚', 2);
        setCookie('xiao', '威龙', 3);

        // 2.获取cookie
        function getCookie(key) {
            // 获取所有的cookie
            console.log(document.cookie);
            let cookieStr = document.cookie;
            // 设置保存cookie值得对象
            let cookieObj = {};
            let cookieArr = cookieStr.split('; ');
            // 对所有cookie进行循环
            cookieArr.forEach(value => {
                let strArr = value.split('=');
                cookieObj[strArr[0]] = strArr[1];
            });
            console.log(cookieObj);
            return cookieObj[key];
        };
        console.log(getCookie('peng'));
        // 3.删除cookie
        function removeCookie(key) {
            setCookie(key, '0', -1);
        };

        removeCookie('xiao');
    </script>
</body>

</html>